//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：FileSearch
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================

using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace HDMCode.FrameEditor
{
	public static class HDMEditorFileIO
    {
        /// <summary>
        /// 文件夹扫描迭代器（异步）
        /// </summary>
        /// <param name="path"> 需要扫描的文件夹根目录 </param>
        /// <param name="result"> 扫描结果的回调 </param>
        public static async Task<List<(string assetsPath,string fileFormat,string fileName)>> GetAllFile(string path = null)
        {
            return await Task.Run(() => 
            {
                if (string.IsNullOrEmpty(path))
                    path = $"{System.Environment.CurrentDirectory}/Assets";

                List<(string assetsPath, string fileFormat, string fileName) > files = new List<(string assetsPath, string fileFormat,string fileName)>();
                Search(path);
                void Search(string taskPath = null) 
                {
                    //获取当前根目录
                    DirectoryInfo topFloder = new DirectoryInfo(taskPath);
                    //获取当前目录下的文件夹
                    DirectoryInfo[] allFloder = topFloder.GetDirectories();
                    //获取当前目录下的文件
                    FileInfo[] fileInfos = topFloder.GetFiles();

                    //获取所有文件
                    for (int i = 0; i < fileInfos.Length; i++)
                    {
                        if (fileInfos[i].Name.EndsWith(".meta"))
                        {
                            continue;
                        }
                        //当前文件格式
                        string suffix = fileInfos[i].Extension;
                        //文件名
                        string fileName = fileInfos[i].Name;
                        //获取扫描文件的Asstes路径
                        string asstesPath = $"{GetAssetsPath(fileInfos[i].DirectoryName)}/{fileInfos[i].Name}";
                        //文件信息写入链表
                        files.Add((asstesPath, suffix, fileName));
                    }

                    //获取所有文件夹
                    foreach (var item in allFloder)
                    {
                        Search(item.FullName);
                    }
                }
                return files;
            });
        }


        /// <summary>
        /// 文件夹扫描迭代器（同步）
        /// </summary>
        /// <param name="path"> 需要扫描的文件夹根目录 </param>
        /// <param name="result"> 扫描结果的回调 </param>

        public static List<(string assetsPath, string fileFormat, string fileName)> GetSyncAllFile(string path = null)
        {
            if (string.IsNullOrEmpty(path))
                path = $"{System.Environment.CurrentDirectory}/Assets";

            List<(string assetsPath, string fileFormat, string fileName)> files = new List<(string assetsPath, string fileFormat, string fileName)>();
            Search(path);
            void Search(string taskPath = null)
            {
                //获取当前根目录
                DirectoryInfo topFloder = new DirectoryInfo(taskPath);
                //获取当前目录下的文件夹
                DirectoryInfo[] allFloder = topFloder.GetDirectories();
                //获取当前目录下的文件
                FileInfo[] fileInfos = topFloder.GetFiles();

                //获取所有文件
                for (int i = 0; i < fileInfos.Length; i++)
                {
                    if (fileInfos[i].Name.EndsWith(".meta"))
                    {
                        continue;
                    }
                    //当前文件格式
                    string suffix = fileInfos[i].Extension;
                    //文件名
                    string fileName = fileInfos[i].Name;
                    //获取扫描文件的Asstes路径
                    string asstesPath = $"{GetAssetsPath(fileInfos[i].DirectoryName)}/{fileInfos[i].Name}";
                    //文件信息写入链表
                    files.Add((asstesPath, suffix, fileName));
                }

                //获取所有文件夹
                foreach (var item in allFloder)
                {
                    Search(item.FullName);
                }
            }
            return files;
        }


        /// <summary>
        /// 过滤文件格式
        /// </summary>
        /// <param name="files"> 文件列表 </param>
        /// <param name="filtration"> 过滤内容 </param>
        public static async Task<List<(string assetsPath, string fileFormat, string fileName)>> FiltrationFormat(List<(string assets, string extension, string fileName)> files, string[] filtrations)
        {
            return await Task.Run(() =>
            {
                List<(string assets, string extension, string fileName)> newFiles = new List<(string assets, string extension, string fileName)>();
                foreach (var file in files)
                {
                    bool isWrite = true;
                    foreach (var filtration in filtrations)
                    {
                        isWrite = file.fileName.Contains(filtration);
                    }
                    if (isWrite)
                        newFiles.Add(file);
                }
                return newFiles;
            });
        }

        /// <summary>
        /// 将路径全局路径转为Assets路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetAssetsPath(string path)
        {
            string[] change = path.Split('\\');
            string outChange = "";
            bool isSave = false;
            foreach (var item in change)
            {
                if (!isSave)
                {
                    if (item == "Assets")
                    {
                        outChange += "Assets";
                        isSave = true;
                    }
                }
                else
                    outChange += "/" + item;
            }
            return outChange;
        }
    }
}


